package com.lz.repository.xjl.impl;

import com.lz.entity.Car;
import com.lz.entity.Customer;
import com.lz.entity.EquipmentSheet;
import com.lz.entity.InstallationPerson;
import com.lz.repository.xjl.CarDao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author 徐建林
 * @date 2018/11/1 12:42
 */
@Repository
public class CarDaoImpl implements CarDao {
    @Autowired
    private SessionFactory sessionFactory;
    @Override
    public void updateCar(Integer equipmentSheetNo, String series, String model, String color, String vin, String carCard) {
        Session session = sessionFactory.openSession();
        //根据equipmentSheetNo得到Car

        EquipmentSheet e = (EquipmentSheet)session.load(EquipmentSheet.class, equipmentSheetNo);
        Car car=e.getCar();
        car.setSeries(series);
        car.setModel(model);
        car.setColor(color);
        car.setVin(vin);
        car.setCarCard(carCard);
        session.update(e);
        session.flush();
        session.close();


    }

    @Override
    public void updateCustomer(Integer equipmentSheetNo, String customerName, String customerTel) {
        Session session = sessionFactory.openSession();
        //根据equipmentSheetNo得到customer

        EquipmentSheet e = (EquipmentSheet)session.load(EquipmentSheet.class, equipmentSheetNo);
        Customer customer=e.getCustomer();

       customer.setCustomerName(customerName);
       customer.setCustomerTel(customerTel);
        session.update(e);
        session.flush();
        session.close();

    }

    @Override
    public void updatePerson(Integer equipmentSheetNo, Integer personNo) {
        Session session = sessionFactory.openSession();
        //根据equipmentSheetNo得到person

        EquipmentSheet e = (EquipmentSheet)session.load(EquipmentSheet.class, equipmentSheetNo);
        InstallationPerson person = e.getNstallationPerson();
        if (personNo!=-1){
            person.setPersonNo(personNo);
        }

        session.update(e);
        session.flush();
        session.close();
    }
    @Override
    public void updateEquipmentSheet(Integer equipmentSheetNo, String comment, Date startTime, Date endingTime) {
        Session session = sessionFactory.openSession();
        //根据equipmentSheetNo得到person

        EquipmentSheet equipmentSheet = (EquipmentSheet)session.load(EquipmentSheet.class, equipmentSheetNo);
        equipmentSheet.setEndingTime(endingTime);
        equipmentSheet.setStartTime(startTime);
        equipmentSheet.setComment(comment);
        session.update(equipmentSheet);
       session.flush();
    session.close();}
}
